package com.koron.css2.clientCenter.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.bean.UnionInfoBean;
import com.koron.css2.clientCenter.bean.UnionInfoDetailBean;
import com.koron.css2.clientCenter.mapper.UnionInfoMapper;
import com.koron.css2.clientCenter.queryBean.UnionInfoQueryBean;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;


public class UnionInfoSave  implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        MessageBean<String> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "保存成功", String.class);
        try {
            UnionInfoBean unionInfoBean = JsonUtils.objectToPojo(req.getData(),UnionInfoBean.class);
            UnionInfoMapper unionInfoMapper = factory.getMapper(UnionInfoMapper.class);

            //传入id为空时新增,不为空进行修改
            if (ObjectUtil.isEmpty(unionInfoBean.getId())) {

                unionInfoBean.setCreateInfo(userInfo);
                unionInfoMapper.add(unionInfoBean);

                //用户分配
                UnionInfoDetailBean detailBean = new UnionInfoDetailBean();
                detailBean.setUnionNo(unionInfoBean.getUnionNo());
                detailBean.setUserNoList(unionInfoBean.getUserNoList());
                unionInfoMapper.unionInfoDeatilUpdate(detailBean);
            }else {
                //根据id查询联合用户信息
                UnionInfoQueryBean queryBean = new UnionInfoQueryBean();
                queryBean.setId(unionInfoBean.getId());
                UnionInfoBean oldBean = unionInfoMapper.queryById(queryBean.getId());
                if (ObjectUtil.isEmpty(oldBean)) {
                    return  MessageBean.create(Constant.MESSAGE_INT_FAIL, "修改失败,未查询到联合用户信息", null);
                }

                //校验修改后的联合编号是否唯一
                queryBean.setId(null);
                queryBean.setUnionNo(unionInfoBean.getUnionNo());
                List<UnionInfoBean> beanList = unionInfoMapper.queryList(queryBean);
                //修改后的联合编号能查询数据，且id不相同时，存在重复联合编号
                if (CollectionUtil.isNotEmpty(beanList) && !beanList.get(0).getId().equals(unionInfoBean.getId())) {
                    return  MessageBean.create(Constant.MESSAGE_INT_FAIL, "修改失败,存在重复联合编号", null);
                }

                //用户分配
                UnionInfoDetailBean detailBean = new UnionInfoDetailBean();

                //根据 原union_no 查询 user_info数据
                List<String> oldUserNoList = unionInfoMapper.userNoQueryList(oldBean.getUnionNo());

                //user_info表存在原 联合编号UnionNo的数据，先将 UnionNo 置为空
                if (CollectionUtil.isNotEmpty(oldUserNoList) ) {
                    //将 原user_info的UnionNo置空
                    detailBean.setUnionNo(null);
                    detailBean.setUserNoList(oldUserNoList);
                    unionInfoMapper.unionInfoDeatilUpdate(detailBean);
                }
                //修改 union_info 表数据
                unionInfoBean.setUpdateInfo(userInfo);
                unionInfoMapper.update(unionInfoBean);

                //修改本次传入user_no的UnionNo
                if (CollectionUtil.isNotEmpty(unionInfoBean.getUserNoList())) {
                    detailBean.setUnionNo(unionInfoBean.getUnionNo());
                    detailBean.setUserNoList(unionInfoBean.getUserNoList());
                    unionInfoMapper.unionInfoDeatilUpdate(detailBean);
                }

            }

            return info;
        }catch (Exception e) {
            logger.error("联合用户保存失败" + e.getMessage(), e);
            return  MessageBean.create(Constant.MESSAGE_INT_FAIL, "保存失败", null);
        }

    }
}
